package com.example.demo.controller;


import com.example.demo.entity.AdminLog;
import com.example.demo.service.AdminLogService;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/log")
public class AdminLogController {

    @Autowired
    private AdminLogService adminLogService;   // 注入登录日志服务

    //根据用户id查找
    @GetMapping("/logs/{admin_id}")
    public List<AdminLog> getByAdminLogsID(@PathVariable Integer admin_id) {
        return  adminLogService.findByAdminId(admin_id);
    }

    //根据日志id查找
    @GetMapping("/logs/{logId}")
    public AdminLog getByLogID(@PathVariable Integer logId) {
        return adminLogService.findByAdminLogId(logId);
    }


    @GetMapping("/logs")
    public List<AdminLog> getAllLogs(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam Integer admin_id,@RequestParam String name) {
        // 获取用户的 IP 地址
        String IP = request.getHeader("x-forwarded-for");
        if (IP == null || IP.length() == 0 || "unknown".equalsIgnoreCase(IP)) {
            IP = request.getHeader("Proxy-Client-IP");
        }
        if (IP == null || IP.length() == 0 || "unknown".equalsIgnoreCase(IP)) {
            IP = request.getHeader("WL-Proxy-Client-IP");
        }
        if (IP == null || IP.length() == 0 || "unknown".equalsIgnoreCase(IP)) {
            IP = request.getRemoteAddr();
        }
        if (IP.equals("0:0:0:0:0:0:0:1")) {
            IP = "127.0.0.1"; // 本地地址处理
        }

        // 获取 User-Agent 信息并解析
        String agentStr = request.getHeader("user-agent");
        System.out.println("User-Agent: " + agentStr); // 打印User-Agent
        UserAgent agent = UserAgent.parseUserAgentString(agentStr); // 使用相应的库解析 User-AgeBrowser
        Browser browser = agent.getBrowser(); // 解析浏览器信息
        OperatingSystem os = agent.getOperatingSystem();

        // 实例化 AdminLog 对象并设置属性
        AdminLog adminLog = new  AdminLog();
        adminLog.setAdmin_id(admin_id);  // 请将其替换为实际的用户ID，通常在用户登录时会有这个值
        adminLog.setName(name);
        adminLog.setLog_Content("用户登录"); // 日志内容
        adminLog.setIP_Address(IP); // IP 地址
        adminLog.setOs(os.getName()); // 操作系统名称
        adminLog.setBrowser(browser.getName()); // 浏览器名称
        adminLog.setCreateDate(new Date()); // 当前时间
        adminLog.setRemark("用户通过登录成功"); // 可选备注
        System.out.println("Inserting Log: " + adminLog);
        Integer result = adminLogService.insert(adminLog); // 确保插入成功
        System.out.println("Insert Result: " + result); // 1 表示成功，0 表示失败

        // 保存登录日志
        adminLogService.insert(adminLog);


        // 返回所有日志数据
        return adminLogService.findAll(); // Assuming this method exists to return all logs


    }

}